//
//  PushAddImageTableViewCell.m
//  BGM
//
//  Created by liuqin on 2018/12/6.
//  Copyright © 2018 com.alirtc.demo. All rights reserved.
//

#import "PushAddImageTableViewCell.h"
#import "Common.h"
@interface PushAddImageTableViewCell()

@property (nonatomic, strong)UIButton *addPhoto;

@end

@implementation PushAddImageTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self != nil) {
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.addPhoto = [[UIButton alloc]initWithFrame:CGRectMake(15, 15, 110, 110)];
        [self.addPhoto setImage:[UIImage imageNamed:@"push_addImage"] forState:UIControlStateNormal];
        self.addPhoto.tag = 99;
        [self addSubview:self.addPhoto];
        [self.addPhoto addTarget:self action:@selector(clickBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}
- (void)setSeleImage:(NSArray *)imageArray;
{
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

    if(imageArray.count==0 ){
        self.addPhoto.frame = CGRectMake(15,15, 110, 110);
        [self addSubview:self.addPhoto];
        return;
    }
    NSMutableArray *resultArray = [[NSMutableArray alloc]init];
    [resultArray addObjectsFromArray:imageArray];
    BOOL isAdd = NO;
    if (resultArray.count < 9 ) {
        [resultArray addObject:@""];
        isAdd = YES;
    }else{
        isAdd = NO;
    }
    float w = (SCREENW-3*110)/3+110;
    
    for(int i = 0;i< resultArray.count;i++) {
        int x = i%3;
        int y = i/3;
        if (isAdd && i == resultArray.count-1) {
            self.addPhoto.frame = CGRectMake(w*x+8,y*130, 110, 110);
            [self addSubview:self.addPhoto];
        }else{
             UIImageView *photoImage = [[UIImageView alloc]initWithFrame:CGRectMake(w*x+8,y*130, 110, 110)];
            photoImage.layer.masksToBounds = YES;
            photoImage.layer.cornerRadius = 5;
            photoImage.backgroundColor = [UIColor redColor];
            photoImage.contentMode = UIViewContentModeScaleAspectFill;
            [photoImage setImage:[resultArray objectAtIndex:i]];
            [self addSubview:photoImage];
            photoImage.userInteractionEnabled = YES;
            UIButton *btn =[[ UIButton alloc]initWithFrame:CGRectMake(80, 0, 30, 30)];
            [btn setBackgroundImage:[UIImage imageNamed:@"deleImage"] forState:UIControlStateNormal];
            btn.tag = i;
            [photoImage addSubview:btn];
            [btn addTarget:self action:@selector(clickDele:) forControlEvents:UIControlEventTouchUpInside];
            
        }
    }
    
}
- (void)clickDele:(UIButton *)btn{
    [self.photoDelegate deleAddImage:btn];
}
- (void)clickBtn{
    [self.photoDelegate clickAddImage];
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
